package org.googlecode.gwt.wii.memory.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;

public class Tessera extends Composite {

	private final static String[] images = new String[]{
		"images/aglioeol.gif",
		"images/agliowee.gif",
		"images/alavino0.gif",
		"images/alcacone.gif",
		"images/antonnoc.gif",
		"images/johnlemo.gif",
		"images/mapoleon.gif",
		"images/tutanpan.gif"
	};
	
	private final FocusPanel container = new FocusPanel();
	private final Image frontImage;
	private final Image rearImage;
	private final int imageNumber;
	
	private boolean frontSide; 
	private boolean freezed = false;
	
	/**
	 * @wbp.parser.constructor
	*/
	public Tessera() {
		this(0);
	}

	public Tessera(int imageNumber) {
		this("images/fragola.jpg", imageNumber);
	}
	
	public Tessera(String front, int imageNumber) {
		super();
		
		this.imageNumber = imageNumber;
		
		this.frontImage = new Image(front);
		frontImage.setSize("100", "100");

		this.rearImage = new Image(images[imageNumber]);
		this.rearImage.setSize("100", "100");
		
		container.setWidget(frontImage);
		container.setSize("100px", "100px");

		frontSide = true;
		
		initWidget(container);
	}

	public boolean flip() {
		
		if( freezed ) {
			return false;
		}
		
		if( frontSide ){
			switchToRear();
		}
		else {
			switchToFront();			
		}
		
		return frontSide;
	}

	private void switchToFront() {
		container.setWidget(frontImage);
		frontSide = true;
	}

	private void switchToRear() {
		container.setWidget(rearImage);
		frontSide = false;
	}
	
	public void freeze() {
		freezed = true;
	}
	
	public void reset() {
		freezed = false;
		
		switchToFront();
	}
	
	public int getImageNumber() {
		return imageNumber;
	}
}
